package com.ler.yrmall.utils;

import cn.hutool.core.bean.BeanUtil;
import com.ler.yrmall.dto.UserDTO;
import com.ler.yrmall.dto.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Leron
 * @version 1.0
 */
public class SecurityUtils {

    public static LoginUser getLoginUser() {
        Object principal = getAuthentication().getPrincipal();
        if (principal instanceof String) {
            throw new RuntimeException("未授权");
        }
        UserDTO userDTO = (UserDTO) principal;
        return BeanUtil.copyProperties(userDTO,LoginUser.class);
    }

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

}
